﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.Diagnostics;

namespace BikePartsManager
{
    class PdfManager
    {
        #region Attributes

        private const string _pathFile = "./export.pdf";
        private const string _watermark = "BikeParts Manager";
        private XGraphics _gfx;
        private PdfDocument _document;
        private List<PdfPage> _pages;

        #endregion

        #region Methods

        public PdfManager()
        {
            _document = new PdfDocument();
            _document.Info.Title = "Export BikeParts Manager";

            _pages = new List<PdfPage>();
            _pages.Add(_document.AddPage());
            var page = _pages[_pages.Count - 1];
        }

        public void Save()
        {
            _document.Save(_pathFile);
        }

        public void Export(string pathFile, ref DataBase db)
        {
            //Create this fucking GFX
            _gfx = XGraphics.FromPdfPage(_pages[_pages.Count - 1], XGraphicsPdfPageOptions.Append);
      
            #region Print WaterMark

            XFont fontWaterMark = new XFont("Verdana", 1, XFontStyle.BoldItalic);

            // Get the size (in point) of the text
            XSize size = _gfx.MeasureString(_watermark, fontWaterMark);

            // Define a rotation transformation at the center of the page
            _gfx.TranslateTransform(_pages[_pages.Count - 1].Width / 4, _pages[_pages.Count - 1].Height / 1.1);
            _gfx.RotateTransform(-Math.Atan(_pages[_pages.Count - 1].Height / _pages[_pages.Count - 1].Width) * 180 / Math.PI);
            _gfx.TranslateTransform(-_pages[_pages.Count - 1].Width / 4, -_pages[_pages.Count - 1].Height / 2.4);

            // Create a graphical path
            XGraphicsPath path = new XGraphicsPath();

            // Add the text to the path
            path.AddString(_watermark, fontWaterMark.FontFamily, XFontStyle.BoldItalic, 75,
              new XPoint((_pages[_pages.Count - 1].Width - size.Width) / 4, (_pages[_pages.Count - 1].Height - size.Height) / 4),
              XStringFormats.Default);

            // Create a dimmed red pen and brush
            XPen pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
            XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));

            // Stroke the outline of the path
            _gfx.DrawPath(pen, brush, path);
            //_gfx.Dispose();

            #endregion

            #region PrintText

            XFont font = new XFont("Verdana", 10, XFontStyle.BoldItalic);

            //_gfx = XGraphics.FromPdfPage(_pages[_pages.Count - 1]);
            //_gfx.DrawString(CreateText(ref db), font, XBrushes.Black,
                //        new XRect(0, 0, _pages[_pages.Count - 1].Width, _pages[_pages.Count - 1].Height));
            //_gfx.Dispose();
            _document.Save(pathFile);

            #endregion
        }

        public void ExportDefaultPath(ref DataBase db)
        {
            Export(_pathFile, ref db);
        }



        private string CreateText(ref DataBase db)
        {
            string buffer = "";

            foreach (User user in db.Users)
            {
                buffer += "User : " + user.Name + " " + user.FirstName + "\n";
                foreach (Bike bike in user.Bikes)
                {
                    buffer += "Bike : " + bike.Brand + " " + bike.Model + " " + "\n";
                    foreach (Part part in bike.Parts)
                    {
                        buffer += "Parts : " + part.Brand + " " + part.Name + " " + part.Price + " " + part.Info +  "\n";
                    }
                }
            }
            Console.WriteLine(buffer);
            return buffer;
        }

        #endregion
    }
}
